/**
 * 
 */
package cn.crm.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.stereotype.Repository;

import cn.crm.bean.InOutLog;
import cn.crm.dao.InOutLogDao;
import cn.crm.service.InOutLogService;
import cn.crm.util.SplitPageManage;

/**
 * @author wbh
 * 
 */
@Repository("InOutLogService")
public class InOutLogServiceImpl implements InOutLogService {
	@Resource
	private InOutLogDao logDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.InOutLogService#save(cn.crm.bean.InOutLog)
	 */
	@Override
	public boolean save(InOutLog log) {
		// TODO Auto-generated method stub
		log.setIolPsid(UUID.randomUUID().toString());
		log.setIolLogtime(new Date());
		log.setIolIsdelete(false);
		return logDao.save(log) != "";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.InOutLogService#del(cn.crm.bean.InOutLog)
	 */
	@Override
	public boolean del(InOutLog log) {
		// TODO Auto-generated method stub
		log.setIolIsdelete(true);
		return logDao.del(log) != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.InOutLogService#upd(cn.crm.bean.InOutLog)
	 */
	@Override
	public boolean upd(InOutLog log) {
		// TODO Auto-generated method stub
		log.setIolLogtime(new Date());
		log.setIolIsdelete(false);
		return logDao.upd(log) != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.InOutLogService#show(cn.crm.bean.InOutLog)
	 */
	@Override
	public JSONArray show(Integer currpage, Integer pagesize, Object... params) {
		// TODO Auto-generated method stub
		List<InOutLog> list = logDao.show(SplitPageManage.getStartIndex(pagesize, currpage), pagesize, params);
		params = null;
		JSONArray array = new JSONArray();
		for (InOutLog iolog : list) {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", iolog.getIolPsid());
			jsonObject.put("inouttype", iolog.getIolInouttype());
			jsonObject.put("user", iolog.getUser().getUsEmpname());
			jsonObject.put("time", iolog.getIolLogtime().toString());
			jsonObject.put("customer", iolog.getCustomer().getCustName());
			jsonObject.put("count", iolog.getIolBycount());
			jsonObject.put("divmoney", iolog.getIolDivmoney());
			jsonObject.put("isok", iolog.getIolIsreceivables());
			jsonObject.put("pname", iolog.getProduct().getPdProductname());
			jsonObject.put("price", iolog.getProduct().getPdPrice());
			array.add(jsonObject);
		}

		return array;
	}

	@Override
	public int count() {
		// TODO Auto-generated method stub
		return logDao.count();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.crm.service.InOutLogService#get(java.lang.String)
	 */
	@Override
	public InOutLog get(String id) {
		// TODO Auto-generated method stub
		return logDao.get(id);
	}

	/**
	 * @param logDao
	 *            the logDao to set
	 */
	public void setLogDao(InOutLogDao logDao) {
		this.logDao = logDao;
	}

	@Override
	public int getcount(Object... params) {
		// TODO Auto-generated method stub
		return logDao.getcount(params);
	}

}
